package com.mycompany.stock;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mycompany.database.Database;
import com.mycompany.stock.dao.StockDao;
import com.mycompany.stock.dto.Stock;
import com.mycompany.tools.Page;

public class StockAction extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public StockAction() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Page page = new Page();
		String currentPage = request.getParameter("currentPage");
		int pageSize = 30;
		if(currentPage==null){
			currentPage = "0";
		}
		page.setCurrentPage(Integer.parseInt(currentPage));
		
		StockDao dao = new StockDao();
		Connection conn =null;
		Stock stock = new Stock();
		String  stockType = request.getParameter("stockType");
		String  stockCode = request.getParameter("stockCode");
		stock.setStockType(stockType);
		stock.setStockCode(stockCode);
		try {
			conn = Database.getConnection();
			dao.setConnection(conn);
			
			page = dao.listStockPage(stock, page);
		
		}catch(Exception e){
			e.printStackTrace();
		}
		request.setAttribute("page",page);
		request.setAttribute("stockType", stockType);
		request.setAttribute("stockCode", stockCode);
		//response.sendRedirect(request.getContextPath()+"/resultList.jsp");
		request.getRequestDispatcher("/resultList.jsp").forward(request,response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occure
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
